//
//  ByUISafetyOrderRecordVC.m
//  chequanbao_ios
//
//  Created by pro on 2017/6/20.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUISafetyOrderRecordVC.h"

// vc
#import "ByUISafetyCerUploadVC.h"       // 上传凭证

// cell
#import "ByCellSafetyOrder.h"

// api
#import "ByApiSafety.h"

// model
#import "ByModelSafetyOrder.h"

@interface ByUISafetyOrderRecordVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *table;

@property (nonatomic, strong) NSMutableArray *items;

@end

@implementation ByUISafetyOrderRecordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"车险订单";
    [self addLeftBackBarButtonItem];
    
    // 模拟数据
//    [self.items addObject:@""];
//    [self.items addObject:@""];
//    [self.items addObject:@""];
//    [self.items addObject:@""];
//    [self.items addObject:@""];
//    [self.items addObject:@""];
//    [self.items addObject:@""];
//    [self.items addObject:@""];
//    [self.items addObject:@""];
    [self createTableView];
    [self.table.mj_header beginRefreshing];

    
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
//    [self fetchData];

}

#pragma mark -- 获取数据
- (void)fetchData{
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    model.uid = BY_USER.uid;
    [self performSelector:@selector(afterLoad) withObject:nil afterDelay:0.01];
    [ByApiSafety getSafetyOrderRecordList:model success:^(ByNetBaseResult *result) {
        if (self.items.count != 0) {
            [self.items removeAllObjects];
        }
        for (ByModelSafetyOrder *model in result.data) {
            [self.items addObject:model];
        }
        [self.table.mj_header endRefreshing];
        [self.table reloadData];
        [ByCoreToolAlert hideHud];
       
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
}

-(void)afterLoad{
    [ByCoreToolAlert showLoadind:@"加载中..."];

}

#pragma mark - 懒加载
- (NSMutableArray *)items {
    
    if (!_items) {
        _items = [NSMutableArray new];
    }
    return _items;
}

#pragma mark - 创建视图
// 创建表视图
- (void)createTableView {
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    self.table.backgroundColor = [UIColor clearColor];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    
    self.table.tableFooterView = [UIView new];
    
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        [self fetchData];
    }];
    self.table.mj_header = header;
}

#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.items.count;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ByCellSafetyOrder *cell = [tableView dequeueReusableCellWithIdentifier:@"ByCellSafetyOrder"];
    if (cell == nil) {
        cell = [[ByCellSafetyOrder alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ByCellSafetyOrder"];
    }
    ByModelSafetyOrder *model = self.items[indexPath.section];
    cell.model = model;
    cell.uploadBlock = ^ {
        ByUISafetyCerUploadVC *vc = [[ByUISafetyCerUploadVC alloc] init];
        vc.insurance_id = model.insurance_id;
        [self by_normalPush:vc hasNavi:YES];
    };

    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60 + 20 + 20;
}

// 设置组尾高
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

// 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
}


@end
